// pages/coach/mDetail/index.js
import api from "../../../api/index";
const dayjs = require("dayjs");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    earningsId: null,
    userId: null,
    currentDate: null,
    maxDate: null,
    data: null,
    coach_info: null,

    initialize: false,
    isLoading: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      userId: options.userid,
      currentDate: dayjs(options.month).format("YYYY-MM"),
      maxDate: dayjs().subtract(1, "month").format("YYYY-MM"),
    });
    this.getDateList();
  },
  getDateList() {
    return new Promise((resolve, reject) => {
      api
        .apiStoreMonthlyInfo({
          coach_id: this.data.userId,
          month: this.data.currentDate,
        })
        .then((res) => {
          if(res.data.coach_info){
            res.data.coach_info.tagStr = res.data.coach_info.depict;
            res.data.coach_info.tagVal = 1;
            this.setData({
              coach_info: res.data.coach_info,
            });
          }
         
          this.setData({
            data: res.data,
          });
        })
        .finally((_) => {
          this.setData({
            initialize: true,
          });
          resolve();
        });
    });
  },
  async switchDate(e) {
    let { maxDate, currentDate, isLoading } = this.data;
    let type = +e.currentTarget.dataset.type;
    if (type === 1 && maxDate <= currentDate) {
      wx.showToast({
        title: "未生成数据",
        icon: "none",
      });
      return;
    }
    if (isLoading) return;
    this.setData({
      isLoading: true,
      currentDate:
        type === 1
          ? dayjs(currentDate).add(1, "month").format("YYYY-MM")
          : dayjs(currentDate).subtract(1, "month").format("YYYY-MM"),
    });
    wx.showLoading({
      title: "加载中...",
      mask: true
    });
    await this.getDateList();
    wx.hideLoading();
    this.setData({
      isLoading: false,
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
